package ui.activity.company;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import controller.company.AllCompanyFeedAsync;
import controller.company.DownloadFileAsync;
import controller.company.DownloadImageInPreviewAllCompanyFeedAsync;
import define.API;
import define.Key;

/**
 * Created by Dong Le on 5/27/2015.
 */
public class PreviewAllCompanyFeed extends Activity implements View.OnClickListener, View.OnTouchListener{

    //string session
    private FrameLayout mFlInPreviewAllCompanyFeed;
    private ImageButton mIbtnPlayVideoInPreviewAllCompanyFeed;
    private ImageButton mIbtnBackInPreviewAllCompanyFeed;
    private ImageView mIvPhotoInPreviewAllCompanyFeed;
    private ImageView mIvPreviewVideoInPreviewAllCompanyFeed;
    private LinearLayout mLlTopInPreviewAllCompanyFeed;
    private LinearLayout mLlBottomInPreviewAllCompanyFeed;
    private TextView mTvDownloadInPreviewAllCompanyFeed;
    private TextView mTvPositionOfFileInPreviewAllCompanyFeed;
    private TextView mTvUserNameInPreviewAllCompanyFeed;
    private VideoView mVvVideoInPreviewAllCompanyFeed;
    //the others session
    Handler mHandler = new Handler();

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            hideTopAndBottomLlInPreviewAllCompanyFeed();
            mHandler.removeCallbacks(this);
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back_in_preview_all_company_feed_activity:
                finish();
                break;
            case R.id.tv_download_in_preview_all_company_feed_activity:
                // TODO download feature on here
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                    new DownloadFileAsync(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                            AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                                    getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                                    getPreviewMedium());
                else
                    new DownloadFileAsync(this).execute(
                            AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                                    getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                                    getPreviewMedium());
                break;
            case R.id.ibtn_play_video_in_preview_all_company_feed_activity:
                mIvPreviewVideoInPreviewAllCompanyFeed.setVisibility(View.GONE);
                mIbtnPlayVideoInPreviewAllCompanyFeed.setVisibility(View.GONE);
                mVvVideoInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
                String URL = "";
                if (API.IS_DEV_SITE) {
                    URL =  AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                            getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                            getPreviewMedium().replace("https","http");
                } else {
                    URL = AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                            getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                            getPreviewMedium();
                }
                playVideoInPreviewAllCompanyFeed(URL);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_all_company_feed);
        initialView();
        initialData();
        setDataForPreviewAllCompanyFeed();
        hideTopAndBottomLlInPreviewAllCompanyFeed();
    }

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        switch (v.getId()) {
            case R.id.fl_in_preview_all_company_feed_activity:
                showTopAndBottomLlInPreviewAllCompanyFeed();
                mHandler.postDelayed(mRunnable, 3000);
                break;
        }
        return false;
    }

    private void initialView () {
        mFlInPreviewAllCompanyFeed = (FrameLayout) findViewById(R.id.fl_in_preview_all_company_feed_activity);
        mIbtnPlayVideoInPreviewAllCompanyFeed = (ImageButton) findViewById(R.id.ibtn_play_video_in_preview_all_company_feed_activity);
        mIbtnBackInPreviewAllCompanyFeed = (ImageButton) findViewById(R.id.ibtn_back_in_preview_all_company_feed_activity);
        mIvPhotoInPreviewAllCompanyFeed = (ImageView) findViewById(R.id.iv_photo_in_preview_all_company_feed_activity);
        mIvPreviewVideoInPreviewAllCompanyFeed = (ImageView) findViewById(R.id.iv_preview_video_in_preview_all_company_feed_activity);
        mLlTopInPreviewAllCompanyFeed = (LinearLayout) findViewById(R.id.ll_top_in_preview_all_company_feed_activity);
        mLlBottomInPreviewAllCompanyFeed = (LinearLayout) findViewById(R.id.ll_bottom_in_preview_all_company_feed_activity);
        mTvDownloadInPreviewAllCompanyFeed = (TextView) findViewById(R.id.tv_download_in_preview_all_company_feed_activity);
        mTvPositionOfFileInPreviewAllCompanyFeed = (TextView) findViewById(R.id.tv_position_of_file_in_preview_all_company_feed_activity);
        mTvUserNameInPreviewAllCompanyFeed = (TextView) findViewById(R.id.tv_user_name_in_preview_all_company_feed_activity);
        mVvVideoInPreviewAllCompanyFeed = (VideoView) findViewById(R.id.vv_video_in_preview_all_company_feed_activity);
    }

    private void initialData () {
        mIbtnBackInPreviewAllCompanyFeed.setOnClickListener(this);
        mTvDownloadInPreviewAllCompanyFeed.setOnClickListener(this);
        mFlInPreviewAllCompanyFeed.setOnTouchListener(this);
        mIbtnPlayVideoInPreviewAllCompanyFeed.setOnClickListener(this);
        // set initial for video view
        MediaController mMc = new MediaController(this);
        mMc.setAnchorView(mVvVideoInPreviewAllCompanyFeed);
        mMc.setMediaPlayer(mVvVideoInPreviewAllCompanyFeed);
        mVvVideoInPreviewAllCompanyFeed.requestFocus();
        mVvVideoInPreviewAllCompanyFeed.setBackgroundColor(Color.BLACK);
        mVvVideoInPreviewAllCompanyFeed.setMediaController(mMc);
        mVvVideoInPreviewAllCompanyFeed.setZOrderOnTop(true);
        mVvVideoInPreviewAllCompanyFeed.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mVvVideoInPreviewAllCompanyFeed.isPlaying()) {
                    mVvVideoInPreviewAllCompanyFeed.pause();
                } else {
                    mVvVideoInPreviewAllCompanyFeed.start();
                }

                return false;
            }
        });
    }

    private void setDataForPreviewAllCompanyFeed () {
        if (AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                getContentType() == Key.photo) {
            mIvPhotoInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
            mIvPreviewVideoInPreviewAllCompanyFeed.setVisibility(View.GONE);
            mIbtnPlayVideoInPreviewAllCompanyFeed.setVisibility(View.GONE);
            mVvVideoInPreviewAllCompanyFeed.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new DownloadImageInPreviewAllCompanyFeedAsync(this, mIvPhotoInPreviewAllCompanyFeed).
                        executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                                AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                                        getPreviewMedium());
            else
                new DownloadImageInPreviewAllCompanyFeedAsync(this, mIvPhotoInPreviewAllCompanyFeed).
                        execute(
                                AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                                        getPreviewMedium());
        } else if (AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                getContentType() == Key.video) {
            mIvPhotoInPreviewAllCompanyFeed.setVisibility(View.GONE);
            mIvPreviewVideoInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
            mIbtnPlayVideoInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
            mVvVideoInPreviewAllCompanyFeed.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new DownloadImageInPreviewAllCompanyFeedAsync(this, mIvPreviewVideoInPreviewAllCompanyFeed).
                        executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                                AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                                        getSplash());
            else
                new DownloadImageInPreviewAllCompanyFeedAsync(this, mIvPreviewVideoInPreviewAllCompanyFeed).
                        execute(
                                AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).
                                        getmAlShotOfCompanyFeedItem().get(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem()).
                                        getSplash());
        }
        mTvPositionOfFileInPreviewAllCompanyFeed.setText(String.valueOf(Enterprise.company.getPositionOfGridViewOfAllCompanyFeedItem() + 1) + " of " +
                String.valueOf(AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).getmAlShotOfCompanyFeedItem().size()));
        mTvUserNameInPreviewAllCompanyFeed.setText("By " + AllCompanyFeedAsync.mAlAllCompanyFeed.get(Enterprise.company.getPositionOfAllCompanyFeedItem()).getUserName());
    }

    private void hideTopAndBottomLlInPreviewAllCompanyFeed () {
        mLlBottomInPreviewAllCompanyFeed.setVisibility(View.GONE);
        mLlTopInPreviewAllCompanyFeed.setVisibility(View.GONE);
    }

    private void showTopAndBottomLlInPreviewAllCompanyFeed () {
        mLlBottomInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
        mLlTopInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
    }

    // play video
    private void playVideoInPreviewAllCompanyFeed(String uri) {
        mVvVideoInPreviewAllCompanyFeed.setBackgroundColor(Color.TRANSPARENT);
        mVvVideoInPreviewAllCompanyFeed.setVideoURI(Uri.parse(uri));

        // Begin play video
        mVvVideoInPreviewAllCompanyFeed.start();
        mVvVideoInPreviewAllCompanyFeed.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // show iv preview video
                mIvPreviewVideoInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
                // show ibtn play video
                mIbtnPlayVideoInPreviewAllCompanyFeed.setVisibility(View.VISIBLE);
                // hide video view
                mVvVideoInPreviewAllCompanyFeed.setVisibility(View.GONE);
            }
        });
    }
}
